package com.zjy.heath_ms.controller;

import com.zjy.heath_ms.entity.Result;
import com.zjy.heath_ms.pojo.Menu;
import com.zjy.heath_ms.service.SystemService;
import com.zjy.heath_ms.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * 项目名 heath_ms
 * 类名  SystemController
 * 作者 lzq
 * 创建时间  2025/5/10 12:36
 */
@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private SystemService systemService;

    /**
     * 判断token是否有效
     * @param token
     * @return
     */
    @GetMapping("/isToken")
    public Result isTokenEffective(String token) {
        Claims claims = JWTUtil.checkJWT(token);
        return Objects.isNull(claims) ? Result.buildFail() : Result.buildSuccess();
    }

    @GetMapping("/userMenus")
    public Result userMenus(HttpServletRequest request) {
        String userId = request.getAttribute("userId").toString();
        List<Menu> menus = systemService.getMenusByUserId(Integer.parseInt(userId));
        return Result.buildSuccess(menus);
    }
}
